package com.google.android.exoplayer2.extractor.ts;

import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.a;
import java.io.IOException;
import o.adq;
import o.adv;
import o.adw;
import o.ady;
import o.adz;
import o.aea;
import o.aea.e;
import o.afy;
import o.agb;
import o.amm;
import o.anc;
import o.ane;
import o.anr;

public final class AdtsExtractor implements Extractor {
    public static final adw ˋ = agb.ॱ;
    private static final int ॱ = anr.ᐝ("ID3");
    private final long ʻ;
    private final ane ʼ;
    @Nullable
    private ady ʽ;
    private final int ˊ;
    private boolean ˊॱ;
    private int ˋॱ;
    private final anc ˎ;
    private final afy ˏ;
    private long ˏॱ;
    private boolean ͺ;
    private boolean ॱˊ;
    private long ॱॱ;
    private final anc ᐝ;

    public AdtsExtractor() {
        this(0);
    }

    public AdtsExtractor(long j) {
        this(j, 0);
    }

    public AdtsExtractor(long j, int i) {
        this.ʻ = j;
        this.ॱॱ = j;
        this.ˊ = i;
        this.ˏ = new afy(true);
        this.ˎ = new anc(2048);
        this.ˋॱ = -1;
        this.ˏॱ = -1;
        this.ᐝ = new anc(10);
        this.ʼ = new ane(this.ᐝ.ˏ);
    }

    public boolean ˎ(adv o_adv) throws IOException, InterruptedException {
        int ˋ = ˋ(o_adv);
        int i = 0;
        int i2 = 0;
        int i3 = ˋ;
        while (true) {
            o_adv.ˎ(this.ᐝ.ˏ, 0, 2);
            this.ᐝ.ˋ(0);
            if (afy.ˎ(this.ᐝ.ॱॱ())) {
                i++;
                if (i >= 4 && i2 > 188) {
                    return true;
                }
                o_adv.ˎ(this.ᐝ.ˏ, 0, 4);
                this.ʼ.ˋ(14);
                int ॱ = this.ʼ.ॱ(13);
                if (ॱ <= 6) {
                    return false;
                }
                o_adv.ˊ(ॱ - 6);
                i2 += ॱ;
            } else {
                o_adv.ˏ();
                i3++;
                if (i3 - ˋ >= 8192) {
                    return false;
                }
                o_adv.ˊ(i3);
                i = 0;
                i2 = 0;
            }
        }
    }

    public void ˏ(ady o_ady) {
        this.ʽ = o_ady;
        this.ˏ.ˏ(o_ady, new a(0, 1));
        o_ady.ˊ();
    }

    public void ˊ(long j, long j2) {
        this.ॱˊ = false;
        this.ˏ.ˏ();
        this.ॱॱ = this.ʻ + j2;
    }

    public void ॱ() {
    }

    public int ˎ(adv o_adv, adz o_adz) throws IOException, InterruptedException {
        boolean z;
        long ॱ = o_adv.ॱ();
        boolean z2 = ((this.ˊ & 1) == 0 || ॱ == -1) ? false : true;
        if (z2) {
            ˊ(o_adv);
        }
        int ˊ = o_adv.ˊ(this.ˎ.ˏ, 0, 2048);
        if (ˊ == -1) {
            z = true;
        } else {
            z = false;
        }
        ˎ(ॱ, z2, z);
        if (z) {
            return -1;
        }
        this.ˎ.ˋ(0);
        this.ˎ.ॱ(ˊ);
        if (!this.ॱˊ) {
            this.ˏ.ˋ(this.ॱॱ, 4);
            this.ॱˊ = true;
        }
        this.ˏ.ˊ(this.ˎ);
        return 0;
    }

    private int ˋ(adv o_adv) throws IOException, InterruptedException {
        int i = 0;
        while (true) {
            o_adv.ˎ(this.ᐝ.ˏ, 0, 10);
            this.ᐝ.ˋ(0);
            if (this.ᐝ.ˏॱ() != ॱ) {
                break;
            }
            this.ᐝ.ˎ(3);
            int ʿ = this.ᐝ.ʿ();
            i += ʿ + 10;
            o_adv.ˊ(ʿ);
        }
        o_adv.ˏ();
        o_adv.ˊ(i);
        if (this.ˏॱ == -1) {
            this.ˏॱ = (long) i;
        }
        return i;
    }

    private void ˎ(long j, boolean z, boolean z2) {
        if (!this.ͺ) {
            boolean z3;
            if (!z || this.ˋॱ <= 0) {
                z3 = false;
            } else {
                z3 = true;
            }
            if (!z3 || this.ˏ.ॱ() != -9223372036854775807L || z2) {
                ady o_ady = (ady) amm.ॱ(this.ʽ);
                if (!z3 || this.ˏ.ॱ() == -9223372036854775807L) {
                    o_ady.ˎ(new e(-9223372036854775807L));
                } else {
                    o_ady.ˎ(ॱ(j));
                }
                this.ͺ = true;
            }
        }
    }

    private void ˊ(adv o_adv) throws IOException, InterruptedException {
        long j = 0;
        if (!this.ˊॱ) {
            this.ˋॱ = -1;
            o_adv.ˏ();
            if (o_adv.ˊ() == 0) {
                ˋ(o_adv);
            }
            int i = 0;
            while (o_adv.ˎ(this.ᐝ.ˏ, 0, 2, true)) {
                this.ᐝ.ˋ(0);
                if (afy.ˎ(this.ᐝ.ॱॱ())) {
                    if (!o_adv.ˎ(this.ᐝ.ˏ, 0, 4, true)) {
                        break;
                    }
                    this.ʼ.ˋ(14);
                    int ॱ = this.ʼ.ॱ(13);
                    if (ॱ > 6) {
                        j += (long) ॱ;
                        i++;
                        if (i != 1000) {
                            if (!o_adv.ˎ(ॱ - 6, true)) {
                                break;
                            }
                        }
                        break;
                    }
                    this.ˊॱ = true;
                    throw new ParserException("Malformed ADTS stream");
                }
                i = 0;
                break;
            }
            o_adv.ˏ();
            if (i > 0) {
                this.ˋॱ = (int) (j / ((long) i));
            } else {
                this.ˋॱ = -1;
            }
            this.ˊॱ = true;
        }
    }

    private aea ॱ(long j) {
        return new adq(j, this.ˏॱ, ˋ(this.ˋॱ, this.ˏ.ॱ()), this.ˋॱ);
    }

    private static int ˋ(int i, long j) {
        return (int) ((((long) (i * 8)) * 1000000) / j);
    }
}
